#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2014年10月29日

@author: chenyongbing
'''

import os,tarfile,sys
import re,time,datetime,commands
import logging,logging.config
logging.basicConfig()
from localConfig import LocalConfig
myLocalConfig = LocalConfig()
current_dir = os.path.split(os.path.realpath(__file__))[0]
sys.path.append(os.path.join(current_dir,'../'))

from base.time_method import myTimeMethod


import logging
logger = logging.getLogger('base.Filter_Rawdata_with_dev')



class FilterData(object):
    '''
    classdocs
    '''


    def __init__(self,day=None):
        '''
        Constructor
        '''
        

        if day==None:
            self.day = (datetime.date.today()-datetime.timedelta(1)).strftime("%Y-%m-%d")
        else:
            self.day = day
        logger = logging.getLogger('Filter_Rawdata(%s)'%(str(self.day)))
        

        
    def run_command(self,cmd):
        rets = commands.getstatusoutput(cmd)
        if rets[0]==0:
             return True
        errMsg =rets[1]
        logger.error('Run cmd %s Faild.'%cmd)
        logger.error(errMsg)
        return False
        
    def filter_by_device(self,rawfile):
        rawpath = os.path.dirname(rawfile)
        rfile = re.sub(rawpath,'',rawfile)
        try:
            os.popen('mkdir %s/tmp'%rawpath)
        except:pass
        
        new_rawfile = rawpath+'/tmp/'+re.sub(rawpath,'',rawfile)
        while 1:
            if os.path.exists(rawfile+'.tar.gz'):
                
                tarcmd = 'tar -xzvf %s -C %s'%(rawfile+'.tar.gz',rawpath+'/tmp/')
                logger.debug(tarcmd)
                if self.run_command(tarcmd):break
            elif os.path.exists('/Udisk/rawdata/%s.tar.gz'%rfile):
                tarcmd = 'tar -xzvf %s -C %s'%('/Udisk/rawdata/%s.tar.gz'%rfile,rawpath+'/tmp/')
                logger.debug(tarcmd)
                if self.run_command(tarcmd):break
            else:
                logger.error('RawFile %s.tar.gz Not Exists.'%rawfile)
                logger.error('RawFile /Udisk/rawdata/%s.tar.gz Not Exists.'%rfile)
                sys.exit()
        os.chdir(rawpath+'/tmp')
        
        logger.debug('rm -rf %s/[!rawdata]*'%(rawpath+'/tmp'))
        self.run_command('rm -rf %s/[!rawdata]*'%(rawpath+'/tmp'))
        result = True
        for new_rawfile in os.listdir(rawpath+'/tmp'):
            cmd = "awk -F '.' '{if ($2!=null) print $0 >>$2}' %s"%new_rawfile
            if not self.run_command(cmd):
                logger.error('Filter rawfile %s Faild.'%rawfile)
                result = False
        return result
    
    
    
    def zip_msg_with_device(self,rawfile='',filter_device=True):
        if filter_device:
            self.filter_by_device(rawfile)
        rawpath = os.path.dirname(rawfile)
        os.chdir(rawpath+'/tmp')
        device_rawpath = rawpath+'/rawdata_device'
        if not os.path.exists(device_rawpath):
            os.mkdir(device_rawpath)
        device_rawpath_day = device_rawpath+'/'+self.day
        if not os.path.exists(device_rawpath_day):
            os.mkdir(device_rawpath_day)
    
        for device in os.listdir(rawpath+'/tmp'):
            cmd = 'tar -czvf %s %s'%(device_rawpath_day+'/'+str(device)+'.txt.tar.gz',device)
            for i in range(5):
                if self.run_command(cmd):break
            if not os.path.exists(device_rawpath_day+'/'+str(device)+'.txt.tar.gz'):
                logger.error('Tar Raw File Faild Device(%s).'%(device))
        if filter_device:
            commands.getstatusoutput('rm -rf %s'%(rawpath+'/tmp'))

  
        
        
if __name__=='__main__':
    import argparse,re,sys
    parser = argparse.ArgumentParser(description='args')
    parser.add_argument('--day',metavar=u"day",default=None)
    parser.add_argument('--device',metavar=u"device",default=None)
    parser.add_argument('--filterType',metavar=u"device",default='device')
    parser.add_argument('--startTime',metavar=u"day",default=None)
    parser.add_argument('--endTime',metavar=u"day",default=None)
    parser.add_argument('--nofilter',action='store_true',help='no filter')
    
    args = parser.parse_args()
    day = args.day
    device = args.device
    filterType = args.filterType
    startTime = args.startTime
    endTime = args.endTime
    nofilter = args.nofilter
    #day = '2014-10-15'
    if day==None:
        day = (datetime.datetime.today()-datetime.timedelta(1)).strftime('%Y-%m-%d')
        days = [day]
    else:
        days = [day]
    if startTime!=None and endTime!=None:
        days = myTimeMethod.get_slist_between_st_et(startTime, endTime)
    for day in days:
        myFilterData = FilterData(day)
    
        
        rawpath = myLocalConfig.rawdata_path
        rawfile = rawpath+'/rawdata.%s.txt'%day
        if filterType=='device':
            filter_device = True
        else:
            filter_device = False
        if nofilter:
            filter_device = False
        myFilterData.zip_msg_with_device(rawfile,filter_device=filter_device)
    
       

    